<?php
class ControllerHkCompanySetup extends Controller {
private $error = array();

	public function index() {
		$this->language->load('hk/company_setup');

		$this->document->setTitle($this->language->get('heading_title'));
		$this->load->model('hk/company_setup');
		// var_dump(111);exit;

		$this->getList();
	}

	public function add() {
		$this->language->load('hk/company_setup');

		$this->document->setTitle($this->language->get('heading_title'));

		$this->load->model('hk/company_setup');
		// var_dump($this->request->post);exit;
		if (($this->request->server['REQUEST_METHOD'] == 'POST') && $this->validateForm()) {
			// var_dump($this->request->post);exit;
			// var_dump($this->request->post,$this->model_hk_company_setup->getProjectFiles($this->request->post['no_1_child']));exit;
			$this->model_hk_company_setup->addCompanySetup($this->request->post);

			$this->session->data['success'] = $this->language->get('text_success');

			$url = '';

			if (isset($this->request->get['sort'])) {
				$url .= '&sort=' . $this->request->get['sort'];
			}

			if (isset($this->request->get['order'])) {
				$url .= '&order=' . $this->request->get['order'];
			}

			if (isset($this->request->get['page'])) {
				$url .= '&page=' . $this->request->get['page'];
			}

			$this->response->redirect($this->url->link('hk/company_setup', 'token=' . $this->session->data['token'] . $url, 'SSL'));
		}

		$this->getForm();
	}

	public function edit() {
		$this->language->load('hk/company_setup');

		$this->document->setTitle($this->language->get('heading_title'));

		$this->load->model('hk/company_setup');

		if (($this->request->server['REQUEST_METHOD'] == 'POST') && $this->validateForm()) {
			// var_dump($this->request->post);exit;
			$this->model_hk_company_setup->editCompanySetup($this->request->get['id'], $this->request->post);

			$this->session->data['success'] = $this->language->get('text_success');

			$url = '';

			if (isset($this->request->get['sort'])) {
				$url .= '&sort=' . $this->request->get['sort'];
			}

			if (isset($this->request->get['order'])) {
				$url .= '&order=' . $this->request->get['order'];
			}

			if (isset($this->request->get['page'])) {
				$url .= '&page=' . $this->request->get['page'];
			}

			$this->response->redirect($this->url->link('hk/company_setup', 'token=' . $this->session->data['token'] . $url, 'SSL'));
		}

		$this->getForm();
	}

	public function delete() {
		$this->language->load('hk/company_setup');

		$this->document->setTitle($this->language->get('heading_title'));

		$this->load->model('hk/company_setup');

		if (isset($this->request->post['selected']) && $this->validateDelete()) {
			foreach ($this->request->post['selected'] as $project_progress_id) {
				$this->model_hk_company_setup->deleteCompanySetup($project_progress_id);
			}

			$this->session->data['success'] = $this->language->get('text_success');

			$url = '';

			if (isset($this->request->get['sort'])) {
				$url .= '&sort=' . $this->request->get['sort'];
			}

			if (isset($this->request->get['order'])) {
				$url .= '&order=' . $this->request->get['order'];
			}

			if (isset($this->request->get['page'])) {
				$url .= '&page=' . $this->request->get['page'];
			}

			$this->response->redirect($this->url->link('hk/company_setup', 'token=' . $this->session->data['token'] . $url, 'SSL'));
		}

		$this->getList();
	}

	public function repair() {
		$this->language->load('hk/company_setup');

		$this->document->setTitle($this->language->get('heading_title'));

		$this->load->model('hk/company_setup');

		if ($this->validateRepair()) {
			$this->model_hk_company_setup->repairCategories();

			$this->session->data['success'] = $this->language->get('text_success');

			$this->response->redirect($this->url->link('hk/company_setup', 'token=' . $this->session->data['token'], 'SSL'));
		}

		$this->getList();
	}

	protected function getList() {
		if (isset($this->request->get['filter_name'])) {
			$filter_name = $this->request->get['filter_name'];
		} else {
			$filter_name = null;
		}

		if (isset($this->request->get['filter_cn_name'])) {
			$filter_cn_name = $this->request->get['filter_cn_name'];
		} else {
			$filter_cn_name = null;
		}

		if (isset($this->request->get['filter_en_name'])) {
			$filter_en_name = $this->request->get['filter_en_name'];
		} else {
			$filter_en_name = null;
		}

		if (isset($this->request->get['filter_status'])) {
			$filter_status = $this->request->get['filter_status'];
		} else {
			$filter_status = null;
		}

		if (isset($this->request->get['filter_email'])) {
			$filter_email = $this->request->get['filter_email'];
		} else {
			$filter_email = null;
		}

		if (isset($this->request->get['filter_tel'])) {
			$filter_tel = $this->request->get['filter_tel'];
		} else {
			$filter_tel = null;
		}

		if (isset($this->request->get['filter_status'])) {
			$filter_status = $this->request->get['filter_status'];
		} else {
			$filter_status = null;
		}


		if (isset($this->request->get['sort'])) {
			$sort = $this->request->get['sort'];
		} else {
			$sort = 'name';
		}

		if (isset($this->request->get['order'])) {
			$order = $this->request->get['order'];
		} else {
			$order = 'DESC';
		}

		if (isset($this->request->get['page'])) {
			$page = $this->request->get['page'];
		} else {
			$page = 1;
		}

		$url = '';
		if (isset($this->request->get['filter_name'])) {
			$url .= '&filter_name=' . urlencode(html_entity_decode($this->request->get['filter_name'], ENT_QUOTES, 'UTF-8'));
		}

		if (isset($this->request->get['filter_email'])) {
			$url .= '&filter_email=' . urlencode(html_entity_decode($this->request->get['filter_email'], ENT_QUOTES, 'UTF-8'));
		}

		if (isset($this->request->get['filter_tel'])) {
			$url .= '&filter_tel=' . urlencode(html_entity_decode($this->request->get['filter_tel'], ENT_QUOTES, 'UTF-8'));
		}

		if (isset($this->request->get['filter_cn_name'])) {
			$url .= '&filter_cn_name=' . urlencode(html_entity_decode($this->request->get['filter_cn_name'], ENT_QUOTES, 'UTF-8'));
		}

		if (isset($this->request->get['filter_en_name'])) {
			$url .= '&filter_en_name=' . urlencode(html_entity_decode($this->request->get['filter_en_name'], ENT_QUOTES, 'UTF-8'));
		}


		if (isset($this->request->get['filter_status'])) {
			$url .= '&filter_status=' . $this->request->get['filter_status'];
		}

		if (isset($this->request->get['sort'])) {
			$url .= '&sort=' . $this->request->get['sort'];
		}

		if (isset($this->request->get['order'])) {
			$url .= '&order=' . $this->request->get['order'];
		}

		if (isset($this->request->get['page'])) {
			$url .= '&page=' . $this->request->get['page'];
		}

		$data['breadcrumbs'] = array();

		$data['breadcrumbs'][] = array(
			'text' => $this->language->get('text_home'),
			'href' => $this->url->link('common/dashboard', 'token=' . $this->session->data['token'], 'SSL')
		);

		$data['breadcrumbs'][] = array(
			'text' => $this->language->get('heading_title'),
			'href' => $this->url->link('hk/company_setup', 'token=' . $this->session->data['token'] . $url, 'SSL')
		);

		$data['add'] = $this->url->link('hk/company_setup/add', 'token=' . $this->session->data['token'] . $url, 'SSL');
		$data['delete'] = $this->url->link('hk/company_setup/delete', 'token=' . $this->session->data['token'] . $url, 'SSL');
		$data['repair'] = $this->url->link('hk/company_setup/repair', 'token=' . $this->session->data['token'] . $url, 'SSL');

		$data['projects'] = array();

		$filter_data = array(
			'filter_cn_name'	  => $filter_cn_name,
			'filter_en_name'	  => $filter_en_name,
			'filter_name' => $filter_name,
			'filter_tel'   => $filter_tel,
			'filter_email'   => $filter_email,
			'filter_status'   => $filter_status,
			'sort'  => $sort,
			'order' => $order,
			'start' => ($page - 1) * $this->config->get('config_limit_admin'),
			'limit' => $this->config->get('config_limit_admin')
		);
		$category_total = $this->model_hk_company_setup->getTotalCompanySetup($filter_data);

		// var_dump($category_total);exit;
		$results = $this->model_hk_company_setup->getCompanySetuplist($filter_data);
		// var_dump($results);exit;
		foreach ($results as $result) {

			$data['company_setup'][] = array(
				'id' => $result['id'],
				'sysid' => (!empty($result['client_id']))?$result['client_id']:'N/A',
				'en_name_1' => $result['en_name1'],
				'cn_name_1' => $result['cn_name1'],
				'en_name_2' => $result['en_name2'],
				'cn_name_2' => $result['cn_name2'],
				'en_name_3' => $result['en_name3'],
				'cn_name_3' => $result['cn_name3'],
				'sec_use' => $result['sec_use'],
				'sec_use_other' => $result['sec_use_other'],
				'sec_addr' => $result['sec_addr'],
				'sec_addr_other' => ($result['sec_addr'] == 'Yes')?$this->model_hk_company_setup->getYbzAddressName($result['sec_addr_other']):$result['sec_addr_other'],
				'name' => $result['name'],
				'email' => $result['email'],
				'tel' => $result['tel'],
				'status' => ($result['status'] != '0')?$this->model_hk_company_setup->getYbzStatus($result['status']):'N/A',
				'addeddate' => $result['addeddate'],
				'edit'        => $this->url->link('hk/company_setup/edit', 'token=' . $this->session->data['token'] . '&id=' . $result['id'] . $url, 'SSL'),
				'delete'      => $this->url->link('hk/company_setup/delete', 'token=' . $this->session->data['token'] . '&id=' . $result['id'] . $url, 'SSL'),
			);
			// var_dump($data['company_setup']);exit;

		}

		$data['heading_title'] = $this->language->get('heading_title');

		$data['text_list'] = $this->language->get('text_list');
		$data['text_no_results'] = $this->language->get('text_no_results');
		$data['text_confirm'] = $this->language->get('text_confirm');

		$data['text_status_1'] = $this->language->get('text_status_1');
		$data['text_status_2'] = $this->language->get('text_status_2');
		$data['text_status_3'] = $this->language->get('text_status_3');
		$data['text_status_4'] = $this->language->get('text_status_4');
		$data['text_status_4'] = $this->language->get('text_status_4');
		$data['text_status_0'] = $this->language->get('text_status_0');

		$data['button_filter'] = $this->language->get('button_filter');

		// $data['column_customer_search'] = $this->language->get('column_customer_search');
		$data['column_sysid'] = $this->language->get('column_sysid');
		$data['column_name'] = $this->language->get('column_name');
		$data['column_email'] = $this->language->get('column_email');
		$data['column_tel'] = $this->language->get('column_tel');
		$data['column_addeddate'] = $this->language->get('column_addeddate');

		$data['column_cn_name'] = $this->language->get('column_cn_name');
		$data['column_en_name'] = $this->language->get('column_en_name');

		$data['column_en_name_1'] = $this->language->get('column_en_name_1');
		//project
		$data['column_cn_name_1'] = $this->language->get('column_cn_name_1');
		$data['column_en_name_2'] = $this->language->get('column_en_name_2');
		$data['column_cn_name_2'] = $this->language->get('column_cn_name_2');
		$data['column_en_name_3'] = $this->language->get('column_en_name_3');
		$data['column_cn_name_3'] = $this->language->get('column_cn_name_3');
		$data['column_sec_use'] = $this->language->get('column_sec_use');
		$data['column_sec_use_other'] = $this->language->get('column_sec_use_other');
		$data['column_sec_addr'] = $this->language->get('column_sec_addr');
		$data['column_sec_addr_other'] = $this->language->get('column_sec_addr_other');

		$data['column_status'] = $this->language->get('column_status');
		$data['column_remarks'] = $this->language->get('column_remarks');
		$data['column_registered_capital'] = $this->language->get('column_registered_capital');
		$data['column_share'] = $this->language->get('column_share');

		$data['button_add'] = $this->language->get('button_add');
		$data['button_edit'] = $this->language->get('button_edit');
		$data['button_delete'] = $this->language->get('button_delete');
		$data['button_rebuild'] = $this->language->get('button_rebuild');


		$data['token'] = $this->session->data['token'];

		if (isset($this->error['warning'])) {
			$data['error_warning'] = $this->error['warning'];
		} else {
			$data['error_warning'] = '';
		}

		if (isset($this->session->data['success'])) {
			$data['success'] = $this->session->data['success'];

			unset($this->session->data['success']);
		} else {
			$data['success'] = '';
		}

		if (isset($this->request->post['selected'])) {
			$data['selected'] = (array)$this->request->post['selected'];
		} else {
			$data['selected'] = array();
		}

		$url = '';



		if (isset($this->request->get['filter_cn_name'])) {
			$url .= '&filter_cn_name=' . urlencode(html_entity_decode($this->request->get['filter_cn_name'], ENT_QUOTES, 'UTF-8'));
		}

		if (isset($this->request->get['filter_en_name'])) {
			$url .= '&filter_en_name=' . urlencode(html_entity_decode($this->request->get['filter_en_name'], ENT_QUOTES, 'UTF-8'));
		}

		if (isset($this->request->get['filter_name'])) {
			$url .= '&filter_name=' . urlencode(html_entity_decode($this->request->get['filter_name'], ENT_QUOTES, 'UTF-8'));
		}

		if (isset($this->request->get['filter_tel'])) {
			$url .= '&filter_tel=' . urlencode(html_entity_decode($this->request->get['filter_tel'], ENT_QUOTES, 'UTF-8'));
		}

		if (isset($this->request->get['filter_email'])) {
			$url .= '&filter_email=' . urlencode(html_entity_decode($this->request->get['filter_email'], ENT_QUOTES, 'UTF-8'));
		}


		if (isset($this->request->get['filter_status'])) {
			$url .= '&filter_status=' . $this->request->get['filter_status'];
		}


		if ($order == 'ASC') {
			$url .= '&order=DESC';
		} else {
			$url .= '&order=ASC';
		}

		if (isset($this->request->get['page'])) {
			$url .= '&page=' . $this->request->get['page'];
		}

		$data['sort_ckg_number'] = $this->url->link('hk/company_setup', 'token=' . $this->session->data['token'] . '&sort=ckg_number' . $url, 'SSL');
		$data['sort_project_name'] = $this->url->link('hk/company_setup', 'token=' . $this->session->data['token'] . '&sort=project_name' . $url, 'SSL');
		$data['sort_status'] = $this->url->link('hk/company_setup', 'token=' . $this->session->data['token'] . '&sort=status' . $url, 'SSL');

		$url = '';

		if (isset($this->request->get['filter_cn_name'])) {
			$url .= '&filter_cn_name=' . urlencode(html_entity_decode($this->request->get['filter_cn_name'], ENT_QUOTES, 'UTF-8'));
		}

		if (isset($this->request->get['filter_en_name'])) {
			$url .= '&filter_en_name=' . urlencode(html_entity_decode($this->request->get['filter_en_name'], ENT_QUOTES, 'UTF-8'));
		}

		if (isset($this->request->get['filter_name'])) {
			$url .= '&filter_name=' . urlencode(html_entity_decode($this->request->get['filter_name'], ENT_QUOTES, 'UTF-8'));
		}

		if (isset($this->request->get['filter_tel'])) {
			$url .= '&filter_tel=' . urlencode(html_entity_decode($this->request->get['filter_tel'], ENT_QUOTES, 'UTF-8'));
		}

		if (isset($this->request->get['filter_email'])) {
			$url .= '&filter_email=' . urlencode(html_entity_decode($this->request->get['filter_email'], ENT_QUOTES, 'UTF-8'));
		}

		if (isset($this->request->get['filter_status'])) {
			$url .= '&filter_status=' . $this->request->get['filter_status'];
		}

		if (isset($this->request->get['sort'])) {
			$url .= '&sort=' . $this->request->get['sort'];
		}



		$pagination = new Pagination();
		$pagination->total = $category_total;
		$pagination->page = $page;
		$pagination->limit = $this->config->get('config_limit_admin');
		$pagination->url = $this->url->link('hk/company_setup', 'token=' . $this->session->data['token'] . $url . '&page={page}', 'SSL');

		$data['pagination'] = $pagination->render();

		$data['results'] = sprintf($this->language->get('text_pagination'), ($category_total) ? (($page - 1) * $this->config->get('config_limit_admin')) + 1 : 0, ((($page - 1) * $this->config->get('config_limit_admin')) > ($category_total - $this->config->get('config_limit_admin'))) ? $category_total : ((($page - 1) * $this->config->get('config_limit_admin')) + $this->config->get('config_limit_admin')), $category_total, ceil($category_total / $this->config->get('config_limit_admin')));

		$data['filter_cn_name'] = $filter_cn_name;
		$data['filter_en_name'] = $filter_en_name;
		$data['filter_name'] = $filter_name;
		$data['filter_tel'] = $filter_tel;
		$data['filter_email'] = $filter_email;
		$data['filter_status'] = $filter_status;

		$data['sort'] = $sort;
		$data['order'] = $order;

		$data['header'] = $this->load->controller('common/header');
		$data['column_left'] = $this->load->controller('common/column_left');
		$data['footer'] = $this->load->controller('common/footer');
		$data['status_all'] = $this->model_hk_company_setup->getProjectStatus();
		// var_dump(111);exit;
		$this->response->setOutput($this->load->view('hk/company_setup_list.tpl', $data));
	}

	protected function getForm() {
		$data['heading_title'] = $this->language->get('heading_title');

		$data['text_form'] = !isset($this->request->get['category_id']) ? $this->language->get('text_add') : $this->language->get('text_edit');
		$data['text_none'] = $this->language->get('text_none');
		$data['text_default'] = $this->language->get('text_default');
		$data['text_enabled'] = $this->language->get('text_enabled');
		$data['text_disabled'] = $this->language->get('text_disabled');

		$data['text_status_1'] = $this->language->get('text_status_1');
		$data['text_status_2'] = $this->language->get('text_status_2');
		$data['text_status_3'] = $this->language->get('text_status_3');
		$data['text_status_4'] = $this->language->get('text_status_4');
		$data['text_status_4'] = $this->language->get('text_status_4');
		$data['text_status_0'] = $this->language->get('text_status_0');

		$data['text_confirm_yes'] = $this->language->get('text_confirm_yes');
		$data['text_confirm_no'] = $this->language->get('text_confirm_no');

		$data['entry_upload'] = $this->language->get('entry_upload');
		$data['entry_overwrite'] = $this->language->get('entry_overwrite');
		$data['entry_progress'] = $this->language->get('entry_progress');

		$data['help_upload'] = $this->language->get('help_upload');

		$data['button_upload'] = $this->language->get('button_upload');
		$data['button_clear'] = $this->language->get('button_clear');
		$data['button_continue'] = $this->language->get('button_continue');

		$data['entry_name'] = $this->language->get('entry_name');
		$data['entry_email'] = $this->language->get('entry_email');
		$data['entry_tel'] = $this->language->get('entry_tel');
		$data['entry_en_name_1'] = $this->language->get('entry_en_name_1');
		$data['entry_cn_name_1'] = $this->language->get('entry_cn_name_1');

		$data['entry_en_name_2'] = $this->language->get('entry_en_name_2');
		$data['entry_cn_name_2'] = $this->language->get('entry_cn_name_2');

		$data['entry_en_name_3'] = $this->language->get('entry_en_name_3');
		$data['entry_cn_name_3'] = $this->language->get('entry_cn_name_3');
		$data['entry_id_card'] = $this->language->get('entry_id_card');


		$data['entry_address_proof'] = $this->language->get('entry_address_proof');

		$data['entry_sec_use'] = $this->language->get('entry_sec_use');
		$data['entry_sec_use_other'] = $this->language->get('entry_sec_use_other');
		$data['entry_sec_addr'] = $this->language->get('entry_sec_addr');
		$data['entry_sec_addr_other'] = $this->language->get('entry_sec_addr_other');

		$data['entry_status'] = $this->language->get('entry_status');
		$data['entry_remarks'] = $this->language->get('entry_remarks');
		$data['entry_registered_capital'] = $this->language->get('entry_registered_capital');
		$data['entry_share'] = $this->language->get('entry_share');


		$data['entry_ds'] = $this->language->get('entry_ds');
		$data['entry_gd'] = $this->language->get('entry_gd');


		$data['button_save'] = $this->language->get('button_save');
		$data['button_cancel'] = $this->language->get('button_cancel');

		$data['tab_general'] = $this->language->get('tab_general');
		$data['tab_data'] = $this->language->get('tab_data');
		$data['tab_design'] = $this->language->get('tab_design');

		if (isset($this->error['warning'])) {
			$data['error_warning'] = $this->error['warning'];
		} else {
			$data['error_warning'] = '';
		}

		
		if (isset($this->error['vendor_quote_3'])) {
			$data['error_vendor_quote_3'] = $this->error['vendor_quote_3'];
		} else {
			$data['error_vendor_quote_3'] = null;
		}


		$url = '';
		if (isset($this->request->get['filter_ckg_number'])) {
			$url .= '&filter_ckg_number=' . urlencode(html_entity_decode($this->request->get['filter_ckg_number'], ENT_QUOTES, 'UTF-8'));
		}

		if (isset($this->request->get['filter_project_name'])) {
			$url .= '&filter_project_name=' . urlencode(html_entity_decode($this->request->get['filter_project_name'], ENT_QUOTES, 'UTF-8'));
		}

		if (isset($this->request->get['filter_english_name_of_supplier'])) {
			$url .= '&filter_english_name_of_supplier=' . urlencode(html_entity_decode($this->request->get['filter_english_name_of_supplier'], ENT_QUOTES, 'UTF-8'));
		}

		if (isset($this->request->get['filter_customer_name']) && isset($this->request->get['filter_customer_value']) ) {
			$url .= '&filter_customer_name=' . urlencode(html_entity_decode($this->request->get['filter_customer_name'], ENT_QUOTES, 'UTF-8'));
			$url .= '&filter_customer_value=' . urlencode(html_entity_decode($this->request->get['filter_customer_value'], ENT_QUOTES, 'UTF-8'));
		}

		if (isset($this->request->get['filter_status'])) {
			$url .= '&filter_status=' . $this->request->get['filter_status'];
		}

		if (isset($this->request->get['sort'])) {
			$url .= '&sort=' . $this->request->get['sort'];
		}

		if (isset($this->request->get['order'])) {
			$url .= '&order=' . $this->request->get['order'];
		}

		if (isset($this->request->get['page'])) {
			$url .= '&page=' . $this->request->get['page'];
		}

		$data['breadcrumbs'] = array();

		$data['breadcrumbs'][] = array(
			'text' => $this->language->get('text_home'),
			'href' => $this->url->link('common/dashboard', 'token=' . $this->session->data['token'], 'SSL')
		);

		$data['breadcrumbs'][] = array(
			'text' => $this->language->get('heading_title'),
			'href' => $this->url->link('hk/company_setup', 'token=' . $this->session->data['token'] . $url, 'SSL')
		);

		if (!isset($this->request->get['id'])) {
			$data['action'] = $this->url->link('hk/company_setup/add', 'token=' . $this->session->data['token'] . $url, 'SSL');
			$data['id'] = '';
		} else {
			$data['action'] = $this->url->link('hk/company_setup/edit', 'token=' . $this->session->data['token'] . '&id=' . $this->request->get['id'] . $url, 'SSL');
			$data['id'] = $this->request->get['id'];
		}

		$data['cancel'] = $this->url->link('hk/company_setup', 'token=' . $this->session->data['token'] . $url, 'SSL');

		$data['company_address'] =  $this->model_hk_company_setup->getYbzAddress();

		if (isset($this->request->get['id']) && ($this->request->server['REQUEST_METHOD'] != 'POST')) {
			$category_info = $this->model_hk_company_setup->getCompanySetup($this->request->get['id']);
		}
		// var_dump($category_info);exit;
		$data['token'] = $this->session->data['token'];

		$this->load->model('localisation/language');

		$data['languages'] = $this->model_localisation_language->getLanguages();

		if (isset($this->request->post['name'])) {
			$data['name'] = $this->request->post['name'];
		} elseif (!empty($category_info)) {
			$data['name'] = $category_info['name'];
		} else {
			$data['name'] = '';
		}

		if (isset($this->request->post['email'])) {
			$data['email'] = $this->request->post['email'];
		} elseif (!empty($category_info)) {
			$data['email'] = $category_info['email'];
		} else {
			$data['email'] = '';
		}

		if (isset($this->request->post['tel'])) {
			$data['tel'] = $this->request->post['tel'];
		} elseif (!empty($category_info)) {
			$data['tel'] = $category_info['tel'];
		} else {
			$data['tel'] = '';
		}

		if (isset($this->request->post['en_name_1'])) {
			$data['en_name_1'] = $this->request->post['en_name_1'];
		} elseif (!empty($category_info)) {
			$data['en_name_1'] = $category_info['en_name1'];
		} else {
			$data['en_name_1'] = '';
		}
		if (isset($this->request->post['cn_name_1'])) {
			$data['cn_name_1'] = $this->request->post['cn_name_1'];
		} elseif (!empty($category_info)) {
			$data['cn_name_1'] = $category_info['cn_name1'];
		} else {
			$data['cn_name_1'] = '';
		}
		if (isset($this->request->post['en_name_2'])) {
			$data['en_name_2'] = $this->request->post['en_name_2'];
		} elseif (!empty($category_info)) {
			$data['en_name_2'] = $category_info['en_name2'];
		} else {
			$data['en_name_2'] = '';
		}
		if (isset($this->request->post['cn_name_2'])) {
			$data['cn_name_2'] = $this->request->post['cn_name_2'];
		} elseif (!empty($category_info)) {
			$data['cn_name_2'] = $category_info['cn_name2'];
		} else {
			$data['cn_name_2'] = '';
		}
		if (isset($this->request->post['en_name_3'])) {
			$data['en_name_3'] = $this->request->post['en_name_3'];
		} elseif (!empty($category_info)) {
			$data['en_name_3'] = $category_info['en_name3'];
		} else {
			$data['en_name_3'] = '';
		}
		if (isset($this->request->post['cn_name_3'])) {
			$data['cn_name_3'] = $this->request->post['cn_name_3'];
		} elseif (!empty($category_info)) {
			$data['cn_name_3'] = $category_info['cn_name3'];
		} else {
			$data['cn_name_3'] = '';
		}		
		if (isset($this->request->post['sec_use'])) {
			$data['sec_use'] = $this->request->post['sec_use'];
		} elseif (!empty($category_info)) {
			$data['sec_use'] = $category_info['sec_use'];
		} else {
			$data['sec_use'] = '';
		}
		if (isset($this->request->post['sec_use_other'])) {
			$data['sec_use_other'] = $this->request->post['sec_use_other'];
		} elseif (!empty($category_info)) {
			$data['sec_use_other'] = $category_info['sec_use_other'];
		} else {
			$data['sec_use_other'] = '';
		}
		if (isset($this->request->post['sec_addr'])) {
			$data['sec_addr'] = $this->request->post['sec_addr'];
		} elseif (!empty($category_info)) {
			$data['sec_addr'] = $category_info['sec_addr'];
		} else {
			$data['sec_addr'] = '';
		}
		if (isset($this->request->post['sec_addr_other'])) {
			$data['sec_addr_other'] = $this->request->post['sec_addr_other'];
		} elseif (!empty($category_info)) {
			$data['sec_addr_other'] = $category_info['sec_addr_other'];
		} else {
			$data['sec_addr_other'] = '';
		}
		if (isset($this->request->post['ds'])) {
			$data['ds'] = $this->request->post['ds'];
		} elseif (!empty($category_info)) {
			$data['ds'] = $category_info['data']['ds'];
		} else {
			$data['ds'] = '';
		}

		if (isset($this->request->post['gd'])) {
			$data['gd'] = $this->request->post['gd'];
		} elseif (!empty($category_info)) {
			$data['gd'] = $category_info['data']['gd'];
		} else {
			$data['gd'] = '';
		}

		if (isset($this->request->post['gd'])) {
			$data['gd'] = $this->request->post['gd'];
		} elseif (!empty($category_info)) {
			$data['gd'] = $category_info['data']['gd'];
		} else {
			$data['gd'] = '';
		}

		if (isset($this->request->post['status'])) {
			$data['status'] = $this->request->post['status'];
		} elseif (!empty($category_info)) {
			$data['status'] = $category_info['status'];
		} else {
			$data['status'] = '';
		}

		if (isset($this->request->post['remarks'])) {
			$data['remarks'] = $this->request->post['remarks'];
		} elseif (!empty($category_info)) {
			$data['remarks'] = $category_info['remarks'];
		} else {
			$data['remarks'] = '';
		}

		if (isset($this->request->post['registered_capital'])) {
			$data['registered_capital'] = $this->request->post['registered_capital'];
		} elseif (!empty($category_info)) {
			$data['registered_capital'] = $category_info['registered_capital'];
		} else {
			$data['registered_capital'] = '';
		}

		$data['domian'] = HTTPS_CATALOG.'project/company_setup/';

		$data['status_all'] = $this->model_hk_company_setup->getProjectStatus();

		$data['header'] = $this->load->controller('common/header');
		$data['column_left'] = $this->load->controller('common/column_left');
		$data['footer'] = $this->load->controller('common/footer');

		$this->response->setOutput($this->load->view('hk/company_setup_form.tpl', $data));
	}

	protected function validateForm() {

		
		
		return !$this->error;
	}

	protected function validateDelete() {
		if (!$this->user->hasPermission('modify', 'hk/company_setup')) {
			$this->error['warning'] = $this->language->get('error_permission');
		}

		return !$this->error;
	}

	protected function validateRepair() {
		if (!$this->user->hasPermission('modify', 'hk/company_setup')) {
			$this->error['warning'] = $this->language->get('error_permission');
		}

		return !$this->error;
	}
	public function autocomplete() {
		$json = array();

		if (isset($this->request->get['filter_ckg_number']) || isset($this->request->get['filter_project_name']) ) {
			$this->load->model('hk/company_setup');
			if (isset($this->request->get['filter_ckg_number'])) {
				$filter_ckg_number = $this->request->get['filter_ckg_number'];
			} else {
				$filter_ckg_number = '';
			}

			if (isset($this->request->get['filter_project_name'])) {
				$filter_project_name = $this->request->get['filter_project_name'];
			} else {
				$filter_project_name = '';
			}

			if (isset($this->request->get['limit'])) {
				$limit = $this->request->get['limit'];
			} else {
				$limit = 5;
			}

			$filter_data = array(
				'filter_ckg_number'  => $filter_ckg_number,
				'filter_project_name' => $filter_project_name,
				'start'        => 0,
				'limit'        => $limit
			);

			$results = $this->model_hk_company_setup->getVendorChecklists($filter_data);
			// var_dump($results);exit;
			foreach ($results as $result) {
				$json[] = array(
					'project_progress_id' => $result['project_progress_id'],
					'ckg_number'        => strip_tags(html_entity_decode($result['ckg_number'], ENT_QUOTES, 'UTF-8')),
					'project_name'        => strip_tags(html_entity_decode($result['project_name'], ENT_QUOTES, 'UTF-8'))
				);
			}
		}



		$this->response->addHeader('Content-Type: application/json');
		$this->response->setOutput(json_encode($json));
	}

	public function autocomplete2() {
		$json = array();

		if (isset($this->request->get['filter_name'])) {
			$this->load->model('hk/company_setup');

			$filter_data = array(
				'filter_name' => $this->request->get['filter_name'],
				'sort'        => 'name',
				'order'       => 'ASC',
				'start'       => 0,
				'limit'       => 5
			);

			$results = $this->model_hk_company_setup->getPojectCustomers($filter_data);

			foreach ($results as $result) {
				$json[] = array(
					'project_customer_id' => $result['project_customer_id'],
					'name'        => strip_tags(html_entity_decode($result['company_eng_name'], ENT_QUOTES, 'UTF-8'))
				);
			}
		}

		$sort_order = array();

		foreach ($json as $key => $value) {
			$sort_order[$key] = $value['name'];
		}

		array_multisort($sort_order, SORT_ASC, $json);

		$this->response->addHeader('Content-Type: application/json');
		$this->response->setOutput(json_encode($json));
	}


	public function autocomplete3() {
		$json = array();

		if (isset($this->request->get['filter_name'])) {
			$this->load->model('hk/company_setup');

			$filter_data = array(
				'filter_name' => $this->request->get['filter_name'],
				'start'       => 0,
				'limit'       => 5
			);

			$results = $this->model_hk_company_setup->getPojectVendor($filter_data);

			foreach ($results as $result) {
				$json[] = array(
					'vendor_details_id' => $result['vendor_details_id'],
					'name'        => strip_tags(html_entity_decode($result['english_name_of_supplier'], ENT_QUOTES, 'UTF-8'))
				);
			}
		}

		$sort_order = array();

		foreach ($json as $key => $value) {
			$sort_order[$key] = $value['name'];
		}

		array_multisort($sort_order, SORT_ASC, $json);

		$this->response->addHeader('Content-Type: application/json');
		$this->response->setOutput(json_encode($json));
	}


	public function upload() {
		$this->language->load('hk/company_setup');
		$this->load->model('hk/company_setup');

		$json = array();

		// Check user has permission
		if (!$this->user->hasPermission('modify', 'hk/company_setup')) {
			$json['error'] = $this->language->get('error_permission');
		}

		// Make sure we have the correct directory
		if (isset($this->request->get['directory'])) {
			$directory = rtrim(DIR_PROJECT . 'project/' . str_replace(array('../', '..\\', '..'), '', $this->request->get['directory']), '/');
		} else {
			$directory = DIR_PROJECT . 'project';
		}

		// Check its a directory
		if (!is_dir($directory)) {
			$json['error'] = $this->language->get('error_directory');
		}

		if (!$json) {
			if (!empty($this->request->files['file']['name']) && is_file($this->request->files['file']['tmp_name'])) {
				// Sanitize the filename
				$filename = basename(html_entity_decode($this->request->files['file']['name'], ENT_QUOTES, 'UTF-8'));

				// Validate the filename length
				if ((utf8_strlen($filename) < 3) || (utf8_strlen($filename) > 128)) {
					$json['error'] = $this->language->get('error_filename');
				}

				// Allowed file extension types
				$allowed = array();
				$FileTypeExt = "png
jpe
jpeg
jpg
pdf
doc";
				$FileTypeMime = "image/png
image/jpeg
application/msword
application/pdf";
				$extension_allowed = preg_replace('~\r?\n~', "\n", $FileTypeExt);

				$filetypes = explode("\n", $extension_allowed);

				foreach ($filetypes as $filetype) {
					$allowed[] = trim($filetype);
				}

				if (!in_array(strtolower(substr(strrchr($filename, '.'), 1)), $allowed)) {
					$json['error'] = $this->language->get('error_filetype');
				}
				// var_dump($this->config->get('config_file_mime_allowed'));exit;
				// Allowed file mime types
				$allowed = array();

				$mime_allowed = preg_replace('~\r?\n~', "\n", $FileTypeMime);

				$filetypes = explode("\n", $mime_allowed);

				foreach ($filetypes as $filetype) {
					$allowed[] = trim($filetype);
				}

				if (!in_array($this->request->files['file']['type'], $allowed)) {
					$json['error'] = $this->language->get('error_filetype');
				}

				// Check to see if any PHP files are trying to be uploaded
				$content = file_get_contents($this->request->files['file']['tmp_name']);

				if (preg_match('/\<\?php/i', $content)) {
					$json['error'] = $this->language->get('error_filetype');
				}

				// Return any upload error
				if ($this->request->files['file']['error'] != UPLOAD_ERR_OK) {
					$json['error'] = $this->language->get('error_upload_' . $this->request->files['file']['error']);
				}
			} else {
				$json['error'] = $this->language->get('error_upload');
			}
		}

		if (!$json) {
			$file = token(16).'_'.$this->request->files['file']['name'];
			$uploadfile = $directory . '/' .$file;
			move_uploaded_file($this->request->files['file']['tmp_name'], $uploadfile);
			$project_file_id = $this->model_hk_company_setup->addFile($filename,$uploadfile);
			$json['filename'] = $file;
			$json['mask'] = $filename;
			$json['uploadfile'] = $uploadfile;
			$json['project_file_id'] = $project_file_id;
			$json['tmp_name'] = $this->request->files['file']['tmp_name'];

			$json['success'] = $this->language->get('text_upload');
		}

		$this->response->addHeader('Content-Type: application/json');
		$this->response->setOutput(json_encode($json));
	}

	public function clear() {
		$this->load->language('hk/company_setup');
		$this->load->model('hk/company_setup');

		$json = array();

		if (!$this->user->hasPermission('modify', 'hk/company_setup')) {
			$json['error'] = $this->language->get('error_permission');
		}

		if (!$json) {

			$file = $this->request->request['name'];
			
			if(!empty($file)){
				$project_progress_id = isset($this->request->request['project_progress_id'])?$this->request->request['project_progress_id']:null;
				$query	= $this->model_hk_company_setup->clearFile($project_progress_id,$this->request->request['id'],$this->request->request['name']);
				 // var_dump($query);
				
				
				if (is_file($query)) {
					unlink($query);
				} elseif (is_dir($query)) {
					rmdir($query);
				}

				$json['success'] = $this->language->get('text_clear');
			}else{
				$json['msg'] = 'No File!';
			}
		}

		$this->response->addHeader('Content-Type: application/json');
		$this->response->setOutput(json_encode($json));
	}
	public function file_upload(){
		$data['token'] = $this->session->data['token'];
		$data['header'] = $this->load->controller('common/header');
		$data['column_left'] = $this->load->controller('common/column_left');
		$data['footer'] = $this->load->controller('common/footer');
		$this->response->setOutput($this->load->view('project/project_upload.tpl', $data));
	}
	public function uploads() {
		$this->language->load('hk/company_setup');
		$this->load->model('hk/company_setup');

		$json = array();

		// Check user has permission
		if (!$this->user->hasPermission('modify', 'hk/company_setup')) {
			$json['error'] = $this->language->get('error_permission');
		}

		// Make sure we have the correct directory
		if (isset($this->request->get['directory'])) {
			$directory = rtrim(DIR_PROJECT . 'company_setup/' . str_replace(array('../', '..\\', '..'), '', $this->request->get['directory']), '/');
		} else {
			$directory = DIR_PROJECT . 'company_setup';
		}

		// Check its a directory
		if (!is_dir($directory)) {
			$json['error'] = $this->language->get('error_directory');
		}

		if (!$json) {
			if (!empty($this->request->files['file']['name']) && is_file($this->request->files['file']['tmp_name'])) {
				// Sanitize the filename
				$filename = basename(html_entity_decode($this->request->files['file']['name'], ENT_QUOTES, 'UTF-8'));

				// Validate the filename length
				if ((utf8_strlen($filename) < 3) || (utf8_strlen($filename) > 128)) {
					$json['error'] = $this->language->get('error_filename');
				}

				// Allowed file extension types
				$allowed = array();
				$FileTypeExt = "png
jpe
jpeg
jpg
pdf
doc";
				$FileTypeMime = "image/png
image/jpeg
application/msword
application/pdf";
				$extension_allowed = preg_replace('~\r?\n~', "\n", $FileTypeExt);

				$filetypes = explode("\n", $extension_allowed);

				foreach ($filetypes as $filetype) {
					$allowed[] = trim($filetype);
				}

				if (!in_array(strtolower(substr(strrchr($filename, '.'), 1)), $allowed)) {
					$json['error'] = $this->language->get('error_filetype');
				}
				// var_dump($this->config->get('config_file_mime_allowed'));exit;
				// Allowed file mime types
				$allowed = array();

				$mime_allowed = preg_replace('~\r?\n~', "\n", $FileTypeMime);

				$filetypes = explode("\n", $mime_allowed);

				foreach ($filetypes as $filetype) {
					$allowed[] = trim($filetype);
				}

				if (!in_array($this->request->files['file']['type'], $allowed)) {
					$json['error'] = $this->language->get('error_filetype');
				}

				// Check to see if any PHP files are trying to be uploaded
				$content = file_get_contents($this->request->files['file']['tmp_name']);

				if (preg_match('/\<\?php/i', $content)) {
					$json['error'] = $this->language->get('error_filetype');
				}

				// Return any upload error
				if ($this->request->files['file']['error'] != UPLOAD_ERR_OK) {
					$json['error'] = $this->language->get('error_upload_' . $this->request->files['file']['error']);
				}
			} else {
				$json['error'] = $this->language->get('error_upload');
			}
		}

		if (!$json) {
			$file = token(16).'_'.$this->request->files['file']['name'];
			$uploadfile = $directory . '/' .$file;
			move_uploaded_file($this->request->files['file']['tmp_name'], $uploadfile);
			// $project_file_id = $this->model_hk_company_setup->addFile($filename,$file);
			$json['filename'] = $file;
			$json['fileurl'] = HTTP_CATALOG.'/project/company_setup/'.$file;
			$json['mask'] = $filename;
			$json['uploadfile'] = $uploadfile;
			// $json['project_file_id'] = $project_file_id;
			$json['tmp_name'] = $this->request->files['file']['tmp_name'];
			$json['code'] = 0;
			$json['success'] = $this->language->get('text_upload');
		}

		$this->response->addHeader('Content-Type: application/json');
		$this->response->setOutput(json_encode($json));
	}
	public function getFiles(){
		$this->load->model('hk/company_setup');
		// $file_ids = ($this->request->post['da'])?$this->request->post['da']：'';
		// $file_id = implode(",",$file_ids);
		if(!empty($this->request->post['da'])){
			$file_id = implode(",",$this->request->post['da']);
			$result = $this->model_hk_company_setup->getFiles($file_id);
			if($result){
				$json = array(
					'status' => 1,
					'msg' => 'success',
					'project_file' => $result,
				);
			}else{
				$json = array(
					'status' =>0,
					'msg' => 'error',
				);
			}
		}else{
			$json = array(
					'status' => 2,
					'msg' => 'error',
				);
		}
		$this->response->addHeader('Content-Type: application/json');
		echo json_encode($json);
	}
}